<?php

function smarty_function_selector( $params, Smarty &$smarty )
{
	foreach( $params as $_k => $_v )
	if( strpos( ',model,assign,append', $_k ) !== false )
	{
		if( strlen( $_v ) ) $$_k = $_v;
	}
	
	
	$_r = '';
	if( isset( $model ) )
	{
		//$_r .= "<select>";
		$m = new $params['model']( );
		$kv = isset($params['value'])?$params['value']:false;
		$kn = isset($params['name'])?$params['name']:false;
		$selected_id = isset($params['selected_id'])?intval($params['selected_id']):null;
		$parent_id = isset($params['parent_id'])?intval($params['parent_id']):null;
		foreach( $m ->getList( $parent_id ) as $id => $d )
		{
			$value = $kv&&isset($d[$kv])?$d[$kv]:reset($d);
			$name = $kn&&isset($d[$kn])?$d[$kn]:(isset($d['name'])?$d['name']:(isset($d['title'])?$d['title']:$value));
			$_r .= sprintf( '<option value="%s"%s>%s</option>', $value, ($selected_id==$id?' selected="selected"':''), $name );
		};
		
		$id = isset( $params['id'] ) ? $params['id'] : ($model.'Selector');
		$name = isset( $params['name'] ) ? $params['name'] : ($model);
		$opt = isset( $params['style'] ) ? (' style="'.$params['style'].'"') : '';
		$_r = sprintf( '<select id="%s" name="%s"%s>%s</select>', $id, $name, $opt, $_r );
	}
	
	//$_html_result = $env->controllers->$controller->exec($action, $attributes);
	
	if( isset( $assign ) )
		$smarty ->assign( $assign, $_r );
	
	elseif( isset( $append ) )
		$smarty ->append( $append, $_r );
	
	else
		return $_r;
}



?>